package com.rent.spider;

import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

/*
@李子宁
Happy,happy everyday!
冲鸭！
2020/8/8

*/
public class WebTheadBeiKe implements Runnable{
    String string;
    Document doc;


    WebTheadBeiKe(String str, Document docc){
        string= str;
        doc= docc;
    }
    private static Logger logger = Logger.getLogger(WebTheadBeiKe.class);

    synchronized public void run() {
        try {
            Element listCon = doc.getElementsByClass("Z_list-box").get(0);

        for (Element element : listCon.children()) {
            try{Thread.sleep(2000);
            //获取房子的图片链接
            String imgUrl = "https:"+element.getElementsByTag("img").attr("data-original");
            //获取房子的具体信息链接
            String houseSingleUrl = "https:"+element.getElementsByTag("a").attr("href");
           //房子的标题
            String title = element.getElementsByClass("title sign").get(0).child(0).html();

            String replace = Html2Text.getContent(element.getElementsByClass("content__list--item--des").toString());
            String[] info = replace.split("/");
            //获取房型：1室2厅
            String singleType = "";
            //获取面积
            String size = "";
            //获取精装，简装
            String direcType = "";
            //获取楼层
            String singleLayer = "";
            //整租，合租，出租类型
            String rentType = "";
            //获取房型
            String place ="";

            if (info.length == 6){
                singleType = info[4];  size = info[2];   singleLayer = info[5];place = info[1];
            }else{
                singleType = info[3];  size = info[1];   singleLayer = info[4];place = info[0];
            }
            direcType = element.getElementsByClass("content__item__tag--decoration").html();

            rentType = title.split("·")[0];


            //秦淮-月牙湖-天坛新村^空房
            Thread.sleep(2000);
            //南京市月牙湖小学
            String underPlace = "";
            underPlace = "暂无信息";
            //房子的价格
            String price = element.getElementsByClass("content__list--item-price").get(0).child(0).html();
            //输出信息到日志
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(imgUrl.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(houseSingleUrl.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(title.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(singleType.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(size.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(direcType.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(singleLayer.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(rentType.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(place.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(underPlace.replace("\t","").replace("\r","").replace("\n","")).append("^")
                    .append(price.replace("\t","").replace("\r","").replace("\n",""));
            logger.info(stringBuilder.toString());
            }catch (IndexOutOfBoundsException e){
                continue;
            }
        }
        }catch (InterruptedException e) {
            e.printStackTrace();
        }



    }
}
